package com.homevip.user.service.impl;

import com.homevip.core.page.Page;
import com.homevip.core.page.Pageable;
import com.homevip.core.util.FastDate;
import com.homevip.user.dao.UserJobDao;
import com.homevip.user.entity.User_job;
import com.homevip.user.service.JobService;
import com.homevip.util.SqlHelper;
import com.homevip.util.StringUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 工作Service实现类
 *
 * @author panpan
 * @version 2014-12-18
 */
@Service("jobService")
public class JobServiceimpl implements JobService {

    @Autowired
    UserJobDao userJobDao;

    @Override
    public User_job getUserJobById(int id) {
        User_job user_job = userJobDao.getById(id);
        return user_job;
    }

    @Override
    @Transactional
    public void saveUserJob(User_job userJob,String writerName) {
        if (0 == userJob.getId()){
            userJob.setCreatedate(new FastDate().toText());
            userJob.setLastdate(new FastDate().toText());
            userJob.setWriter(writerName);
            userJobDao.save(userJob);
        }else{
            userJob.setLastdate(new FastDate().toText());
            userJobDao.update(userJob);
        }
    }

    @Override
    @Transactional
    public void saveUserJob(User_job userJob) {
        if (0 == userJob.getId()){
            userJob.setCreatedate(new FastDate().toText());
            userJob.setLastdate(new FastDate().toText());
            userJobDao.save(userJob);
        }else{
            userJob.setLastdate(new FastDate().toText());

            userJobDao.update(userJob);
        }
    }

    @Override
    @Transactional
    public void deleteUserJob(User_job userJob) {
        userJobDao.delete(userJob);
    }

    @Override
    public Page<User_job> findUserJob(String keyword, Integer status, Pageable pageable) {
        Map<String, Object> params = new HashMap<String, Object>();
        Page<User_job> page = null;

        if(null != status && status > 0){
            params.put("status", status);
        }

        if(!StringUtil.isEmpty(keyword)) {
            keyword = SqlHelper.toJpqlParamLike(keyword);
        }

        page = userJobDao.findByAndLike(params, "jname", keyword, pageable);

        return page;
    }

    @Override
    public List<User_job> findUserJob(String keyword, Integer status) {
        Map<String, Object> params = new HashMap<String, Object>();

        if(null != status && status > 0){
            params.put("status", status);
        }

        if(!StringUtil.isEmpty(keyword)) {
            keyword = SqlHelper.toJpqlParamLike(keyword);
        }

        return userJobDao.findByAndLike(params, "writer", keyword);
    }
    @Override
    public List<User_job> findUserJob(String writer) {
        Map<String, Object> params = new HashMap<String, Object>();

//        if(null != writer && writer !=""){
//            params.put("writer", writer);
//        }

        if(!StringUtil.isEmpty(writer)) {
            writer = SqlHelper.toJpqlParamLike(writer);
        }

        return userJobDao.findByAndLike(params, "writer", writer);
    }

}
